# -*- coding: utf-8 -*-
# @Author    : Tian Wencheng
# @CreateTime: 2021/1/15 11:18
# @FileName  : device_management.py
# @IDE       : PyCharm
# @Git       : https://gitee.com/dianetian/projects


from interface.base import BaseModule
from common.request import HTTPRequest


class DeviceManagementModule(BaseModule):
    def __init__(self):
        self.request_url = ""
        self.request_method = ""
        self.request_headers = {"Content-Type": "application/json"}
        self.request_params = None
        self.cookies = super().cookies
        self.web_url = super().web_url

    def get_stream_server_status(self, **kwargs):
        id = kwargs.get("id")

        self.request_url = "/api/manager/streamServer/alive"
        url = self.web_url + self.request_url
        self.request_method = "GET"
        self.request_params = {"id": id}

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response

    def get_stream_server_info(self, **kwargs):
        name = kwargs.get("name")
        ip = kwargs.get("ip")

        self.request_url = "/api/manager/streamServers"
        url = self.web_url + self.request_url
        self.request_method = "GET"
        self.request_params = {"name": name, "ip": ip}

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response

    def delete_stream_server_info(self, **kwargs):
        ids = kwargs.get("ids")

        self.request_url = "/api/manager/streamServer"
        url = self.web_url + self.request_url
        self.request_method = "DELETE"
        self.request_params = {"ids": ids}

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response

    def add_stream_server_info(self, **kwargs):
        name = kwargs.get("name")
        username = kwargs.get("username")
        password = kwargs.get("password")
        ip = kwargs.get("ip")
        port = kwargs.get("port")
        ex_ip = kwargs.get("exIp")
        ex_port = kwargs.get("exPort")

        self.request_url = "/api/manager/streamServer"
        url = self.web_url + self.request_url
        self.request_method = "POST"
        self.request_params = {"name": name, "username": username, "password": password, "ip": ip, "port": port,
                               "exIp": ex_ip, "exPort": ex_port}

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response

    def update_stream_server_info(self, **kwargs):
        id = kwargs.get("id")
        name = kwargs.get("name")
        username = kwargs.get("username")
        password = kwargs.get("password")
        ip = kwargs.get("ip")
        port = kwargs.get("port")
        ex_ip = kwargs.get("exIp")
        ex_port = kwargs.get("exPort")

        self.request_url = "/api/manager/streamServer"
        url = self.web_url + self.request_url
        self.request_method = "POST"
        self.request_params = {"id": id, "name": name, "username": username, "password": password, "ip": ip,
                               "port": port, "exIp": ex_ip, "exPort": ex_port}

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response

    def update_camera_quality_check_setting(self, **kwargs):
        id = kwargs.get("id")
        vqd_params = kwargs.get("vqd_params")

        self.request_url = "/api/manager/checkSetting"
        url = self.web_url + self.request_url
        self.request_method = "POST"
        self.request_params = {"id": id, "vqd_params": vqd_params}

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response

    def get_camera_quality_detail(self, **kwargs):
        device_name = kwargs.get("deviceNAme")
        ip = kwargs.get("ip")
        malfunction_type = kwargs.get("malfunctionType")
        areas = kwargs.get("areas[]")
        page_size = kwargs.get("pageSize")
        current_page = kwargs.get("currentPage")

        self.request_url = "/api/manager/deviceStatisticsTableData"
        url = self.web_url + self.request_url
        self.request_method = "GET"
        self.request_params = {"deviceName": device_name, "ip": ip, "malfunctionType": malfunction_type,
                               "areas[]": areas, "pageSize": page_size, "currentPage": current_page}

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response

    def delete_unit(self, **kwargs):
        id = kwargs.get("id")

        self.request_url = "/api/manager/node"
        url = self.web_url + self.request_url
        self.request_method = "DELETE"
        self.request_params = {"id": id}

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response

    def get_camera_info(self, **kwargs):
        device_name = kwargs.get("deivceName")
        direction = kwargs.get("direction")
        ip = kwargs.get("ip")
        analysis_function = kwargs.get("analysisFunction")
        device_type = kwargs.get("deviceType")
        camera_ids = kwargs.get("cameraIds")
        stream_server = kwargs.get("streamServer")
        current_page = kwargs.get("currentPage")
        page_size = kwargs.get("pageSize")

        self.request_url = "/api/manager/device"
        url = self.web_url + self.request_url
        self.request_method = "GET"
        self.request_params = {"deivceName": device_name, "direction": direction, "ip": ip,
                               "analysisFunction": analysis_function, "deviceType": device_type,
                               "cameraIds": camera_ids, "streamServer": stream_server, "currentPage": current_page,
                               "pageSize": page_size}

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response

    def get_select_items(self):
        self.request_url = "/api/manager/formDropSelect"
        url = self.web_url + self.request_url
        self.request_method = "GET"

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response

    def delete_camera_info(self, **kwargs):
        ids = kwargs.get("ids")

        self.request_url = "/api/manager/device"
        url = self.web_url + self.request_url
        self.request_method = "DELETE"
        self.request_params = {"ids": ids}

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response

    def add_camera_info(self, **kwargs):
        access_type = kwargs.get("accessType")
        channel = kwargs.get("channel")
        device_name = kwargs.get("deviceName")
        device_type = kwargs.get("deviceType")
        ip = kwargs.get("ip")
        manufacture = kwargs.get("manufacture")
        username = kwargs.get("username")
        password = kwargs.get("password")
        port = kwargs.get("port")
        road_id = kwargs.get("roadId")
        stream_server = kwargs.get("streamserver")
        camera_url = kwargs.get("url")
        camera_sub_url = kwargs.get("subUrl")
        usable_event_ids = kwargs.get("usableEventIds")

        self.request_url = "/api/manager/device"
        url = self.web_url + self.request_url
        self.request_method = "POST"
        self.request_params = {"accessType": access_type, "channel": channel, "deviceName": device_name,
                               "deviceType": device_type, "ip": ip, "manufacture": manufacture, "password": password,
                               "port": port, "roadId": road_id, "streamserver": stream_server, "subUrl": camera_sub_url,
                               "url": camera_url, "username": username, "usableEventIds": usable_event_ids}

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response

    def update_camera_info(self, **kwargs):
        id = kwargs.get("id")
        access_type = kwargs.get("accessType")
        channel = kwargs.get("channel")
        device_name = kwargs.get("deviceName")
        device_type = kwargs.get("deviceType")
        ip = kwargs.get("ip")
        manufacture = kwargs.get("manufacture")
        username = kwargs.get("username")
        password = kwargs.get("password")
        port = kwargs.get("port")
        road_id = kwargs.get("roadId")
        stream_server = kwargs.get("streamserver")
        camera_url = kwargs.get("url")
        camera_sub_url = kwargs.get("subUrl")
        usable_event_ids = kwargs.get("usableEventIds")

        self.request_url = "/api/manager/device"
        url = self.web_url + self.request_url
        self.request_method = "POST"
        self.request_params = {"id": id, "accessType": access_type, "channel": channel, "deviceName": device_name,
                               "deviceType": device_type, "ip": ip, "manufacture": manufacture, "password": password,
                               "port": port, "roadId": road_id, "streamserver": stream_server, "subUrl": camera_sub_url,
                               "url": camera_url, "username": username, "usableEventIds": usable_event_ids}

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response
